home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 June / EnigmA AMIGA RUN 08 (1996)(G.R. Edizioni)(IT)[!][issue 1996-06][EARSAN CD VII].iso / earcd / utmisc1 / chktex.lha / chktex / WB2Argv.c < prev    next >
C/C++ Source or Header  |  1996-04-30  |  7KB  |  261 lines

  1. /*
  2.  *  WB2Argv v1.3, Amiga Workbench argv/argc emulation routines.
  3.  *  Copyright (C) 1995-96 Jens T. Berger Thielemann
  4.  *
  5.  *  This program is free software; you can redistribute it and/or modify
  6.  *  it under the terms of the GNU General Public License as published by
  7.  *  the Free Software Foundation; either version 2 of the License, or
  8.  *  (at your option) any later version.
  9.  *
  10.  *  This program is distributed in the hope that it will be useful,
  11.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  *  GNU General Public License for more details.
  14.  *
  15.  *  You should have received a copy of the GNU General Public License
  16.  *  along with this program; if not, write to the Free Software
  17.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  *
  19.  *  Contact the author at:
  20.  *      Jens Berger
  21.  *      Spektrumvn. 4
  22.  *      N-0666 Oslo
  23.  *      Norway
  24.  *      E-mail: <jensthi@ifi.uio.no>
  25.  *
  26.  *  Note: If you use any of these routines in your programs, you have to
  27.  *  mention so in the document.
  28.  *
  29.  *
  30.  */
  31.  
  32. #ifdef AMIGA
  33.  
  34. #include <stdio.h>
  35. #include <stdlib.h>
  36. #include <string.h>
  37. #include <ctype.h>
  38.  
  39. #include <workbench/startup.h>
  40. #include <proto/dos.h>
  41. #include <workbench/workbench.h>
  42. #include <proto/icon.h>
  43. #include <proto/exec.h>
  44.  
  45. #include "WB2Argv.h"
  46.  
  47. /* #define WB_DEBUG */
  48.  
  49. #ifdef WB_DEBUG
  50. char __stdiowin[] = "CON:0/10/640/180/WB2Argv";
  51. char __stdiov37[] = "/AUTO/CLOSE/WAIT";
  52. #endif
  53.  
  54. static char *ExpandLockName(BPTR Lock, STRPTR Name);
  55.  
  56. /*
  57.  * Converts a message from workbench into an argv look-a-like. Tooltypes
  58.  * are converted to long-named options, suitable for passing to GNU's
  59.  * getopt_long(). Shift-clicked files will be appended at the end; their
  60.  * tooltypes will _currently_ not be processed. This may happen in a future
  61.  * version (is it desirable?).
  62.  *
  63.  * Flags: Currently these do only control whether we'll convert the
  64.  * case of the options. See WB2Argv.h for options.
  65.  *
  66.  * Returns an array of string-pointers; terminated with a NULL. To get
  67.  * argc, call CountArgv(). Returns NULL if out of memory, or not running
  68.  * v2.04.
  69.  *
  70.  */
  71.  
  72.  
  73. char **WB2Argv(struct WBStartup *WBMsg, LONGBITS Flags)
  74. {
  75.     ULONG   c = 0,
  76.             CurArg = 0,
  77.             NewArgc = 0;
  78.     char    **NewArgv,
  79.             **Tools,
  80.             *Buf,
  81.             *Orig;
  82.     BOOL    TmpSucc = TRUE,
  83.             IconOpen = FALSE;
  84.     struct DiskObject *dskobj = NULL;
  85.     BPTR    OldCurDir = 0L;
  86.  
  87.     if(WBMsg && (DOSBase->dl_lib.lib_Version > 36))
  88.     {
  89.         /*
  90.          * First, count the # of argv entries. We get both from
  91.          * shift-clicked icons + tooltypes.
  92.          */
  93.  
  94.         NewArgc = WBMsg->sm_NumArgs;
  95.  
  96.         if(IconBase = OpenLibrary("icon.library", 0))
  97.         {
  98.             OldCurDir = CurrentDir(WBMsg->sm_ArgList->wa_Lock);
  99.             IconOpen = TRUE;
  100.             if(dskobj = GetDiskObject(WBMsg->sm_ArgList->wa_Name))
  101.             {
  102.                 Tools = dskobj->do_ToolTypes;
  103.  
  104.                 while(*Tools++)
  105.                     NewArgc++;
  106.             }
  107.         }
  108.  
  109.         /*
  110.          * Stuff it together...
  111.          */
  112.  
  113.         if(NewArgv = calloc((size_t) NewArgc + 2, sizeof(char *)))
  114.         {
  115.             CurArg = 0;
  116.  
  117.             /* Program name */
  118.  
  119.             TmpSucc = FALSE;
  120.  
  121.             if(Buf = ExpandLockName(WBMsg->sm_ArgList->wa_Lock,
  122.                                     WBMsg->sm_ArgList->wa_Name))
  123.             {
  124.                 NewArgv[CurArg++] = Buf;
  125.                 TmpSucc = TRUE;
  126.             }
  127.  
  128.             if(dskobj && TmpSucc)
  129.             {
  130.                 /* Tooltypes becomes longnamed options */
  131.                 for(Tools = dskobj->do_ToolTypes;
  132.                     (Orig = *Tools) && TmpSucc;
  133.                     Tools++)
  134.                 {
  135.                     Orig = stpblk(Orig);
  136.  
  137.                     /* To avoid a false `--' */
  138.                     if(*Orig && (*Orig != '(')) /* ) */
  139.                     {
  140.                         TmpSucc = FALSE;
  141.  
  142.                         if(Buf = malloc(strlen(Orig) + 4))
  143.                         {
  144.                             strcpy(Buf, "--");
  145.                             strcat(Buf, Orig);
  146.  
  147.                             TmpSucc = TRUE;
  148.                             NewArgv[CurArg++] = Buf;
  149.  
  150.                             /* Make option lowercase */
  151.  
  152.                             switch(Flags & W2A_CASEMASK)
  153.                             {
  154.                             case W2A_LOWER:
  155.                                 for(;
  156.                                     *Buf && (*Buf != '=');
  157.                                     Buf++)
  158.                                     *Buf = tolower(*Buf);
  159.                                 break;
  160.                             case W2A_UPPER:
  161.                                 for(;
  162.                                     *Buf && (*Buf != '=');
  163.                                     Buf++)
  164.                                     *Buf = toupper(*Buf);
  165.                                 break;
  166.                             case W2A_KEEPCASE:
  167.                                 break;
  168.                             }
  169.                         }
  170.                     }
  171.                 }
  172.             }
  173.  
  174.             /* End of options... */
  175.             NewArgv[CurArg++] = "--";
  176.  
  177.             if(TmpSucc)
  178.             {
  179.                 for(c = 1;          /* Skip program name */
  180.                    (c < WBMsg->sm_NumArgs) && TmpSucc;
  181.                     c++)
  182.                 {
  183.                     TmpSucc = FALSE;
  184.  
  185.                     if(Buf = ExpandLockName(WBMsg->sm_ArgList[c].wa_Lock,
  186.                                             WBMsg->sm_ArgList[c].wa_Name))
  187.                     {
  188.                         NewArgv[CurArg++] = Buf;
  189.                         TmpSucc = TRUE;
  190.                     }
  191.                 }
  192.             }
  193.         }
  194.  
  195.         if(dskobj)
  196.             FreeDiskObject(dskobj);
  197.  
  198.         if(OldCurDir)
  199.             CurrentDir(OldCurDir);
  200.  
  201.         if(IconOpen)
  202.             CloseLibrary(IconBase);
  203.  
  204.         if(TmpSucc)
  205.             return(NewArgv);
  206.     }
  207.  
  208.     return(NULL);
  209. }
  210.  
  211. ULONG CountArgv(const char **argv)
  212. {
  213.     ULONG argc = 0;
  214.  
  215.     if(argv)
  216.     {
  217.         while(*argv++)
  218.             argc++;
  219.     }
  220.     return(argc);
  221. }
  222.  
  223. static char *ExpandLockName(BPTR Lock, STRPTR Name)
  224. {
  225.     UBYTE   Buffer[BUFSIZ];
  226.     char    *Retval = NULL;
  227.  
  228.     if(NameFromLock(Lock, Buffer, BUFSIZ))
  229.     {
  230.         if(AddPart(Buffer, Name, BUFSIZ))
  231.             Retval = strdup(Buffer);
  232.     }
  233.  
  234.     return(Retval);
  235. }
  236.  
  237. #ifdef WB_DEBUG
  238. #   define EXIT_FAILURE 20
  239. void main(int argc, char **argv)
  240. {
  241.     int c;
  242.  
  243. #ifdef AMIGA
  244.     if(_WBenchMsg)
  245.     {
  246.         if(argv = WB2Argv(_WBenchMsg, W2A_LOWER))
  247.             argc = CountArgv(argv);
  248.         else
  249.             exit(EXIT_FAILURE);
  250.     }
  251. #endif
  252.  
  253.     for(c = 0; c < argc; c++)
  254.         printf("%s\n", argv[c]);
  255.  
  256. }
  257.  
  258. #endif /* WB_DEBUG */
  259.  
  260. #endif /* AMIGA */
  261.